﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TeachingAffairsManagementSystem.WebControl;

namespace TeachingAffairsManagementSystem.UserControl.RightContent.Student
{
    public partial class LessonMarket : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void LBSearchAll_Click(object sender, EventArgs e)
        {
            LessonDataSource.SelectCommand = "SELECT * FROM view_lesson WHERE finished='否'";
            GVLessonMarket.DataBind();
        }

        protected void LBSearchL_ID_Click(object sender, EventArgs e)
        {
            try
            {
                int l_id = Convert.ToInt32(TBSearch.Text);
                LessonDataSource.SelectCommand =
                    $"SELECT * FROM view_lesson WHERE finished='否' and l_id = {l_id}";
                GVLessonMarket.DataBind();
            }
            catch
            {
            }
        }


        protected void LBSearchName_Click(object sender, EventArgs e)
        {
            string name = TBSearch.Text;
            LessonDataSource.SelectCommand =
                $"SELECT * FROM view_lesson_market  WHERE finished='否' and name like '%{name}%'";
            GVLessonMarket.DataBind();
        }

        protected void GVLesson_OnRowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "SelectLesson":
                    int currentIndex = Convert.ToInt32(e.CommandArgument);
                    int l_id = Convert.ToInt32(GVLessonMarket.DataKeys[currentIndex]["l_id"]);
                    int s_id = Convert.ToInt32(Session["id"]);
                    int t_id = Convert.ToInt32(GVLessonMarket.DataKeys[currentIndex]["teacher_t_id"]);
                    //检查是否可选
                    if (Operation.IsLessonSelectable(s_id, l_id, t_id))
                    {
                        // 选课
                        if (Operation.SelectLesson(s_id, l_id, t_id))
                        {
                            Alert.Show("加入成功", AlertBox.Level.Success, true);
                        }
                        else
                        {
                            Alert.Show("加入失败", AlertBox.Level.Danger, true);
                        }
                    }
                    else
                    {
                        Alert.Show("已加入该课程", AlertBox.Level.Danger, true);
                    }

                    break;
            }
        }
    }
}